package com.tp.ssm.controller;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.core.helpers.UUIDUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.tp.ssm.controller.exception.CustomException;
import com.tp.ssm.controller.validation.VaildGroup1;
import com.tp.ssm.po.ItemsCustom;
import com.tp.ssm.po.ItemsQueryVo;
import com.tp.ssm.service.ItemsService;

@Controller
@RequestMapping("/items")
public class ItemsController {
	
	@ModelAttribute("itemtypes")
	public Map<String, String> getItemTypes(){
		
		Map<String, String> itemTypes = new HashMap<String,String>();
		itemTypes.put("101", "数码");
		itemTypes.put("102", "母婴");
		
		return itemTypes;
	}

	
	private ItemsService itemsService ;
	
	public ItemsService getItemsService() {
		return itemsService;
	}
	
	@Autowired
	public void setItemsService(ItemsService itemsService) {
		this.itemsService = itemsService;
	}

	/*@RequestMapping("/queryItems")
	public ModelAndView queryItems() throws Exception{
		List<ItemsCustom> itemsList = itemsService.findItemsList(null); 
		
		ModelAndView modelAndView = new ModelAndView() ;
		modelAndView.addObject("itemsList",itemsList) ;
		modelAndView.setViewName("items/itemsList");
		
		return modelAndView ;
	}*/
	
	@RequestMapping("/queryItems")
	public String queryItems(Model model,ItemsQueryVo itemsQueryVo) throws Exception{
		List<ItemsCustom> itemsList = itemsService.findItemsList(itemsQueryVo); 
		
		model.addAttribute("itemsList", itemsList) ;
		
		return "items/itemsList";
	}
	
	@RequestMapping(value="/editItems",method={RequestMethod.GET,RequestMethod.POST})
	public ModelAndView editItems(@RequestParam(value="id") Integer items_id) throws Exception{
		
		ItemsCustom itemsCustom = itemsService.findById(items_id);
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("itemsCustom", itemsCustom) ;
		modelAndView.setViewName("items/editItems");
		
		return modelAndView ;
	}
	
	/*@RequestMapping("/editItemsSubmit")
	public ModelAndView editItemsSubmit() throws Exception{
		
		ModelAndView modelAndView = new ModelAndView();
		
		modelAndView.setViewName("success");
		
		return modelAndView ;
	}*/
	
	/*@RequestMapping("/editItemsSubmit")
	public String editItemsSubmit() throws Exception{
		
		return "redirect:queryItems.action" ;
	}*/
	
	@RequestMapping("/editItemsSubmit")
	public String editItemsSubmit(Model model,Integer id,
			@ModelAttribute @Validated(value={VaildGroup1.class}) ItemsCustom itemsCustom,
			BindingResult bindingResult,
			HttpServletRequest request,
			HttpServletResponse response,
			MultipartFile file) throws Exception{
		
		if(bindingResult.hasErrors()){
			List<ObjectError> allErrors = bindingResult.getAllErrors();
			model.addAttribute("allErrors", allErrors) ;
			return "items/editItems" ; 
		}
		//  取得项目的真实路径
		String contextPath = request.getSession().getServletContext().getRealPath("/") ;
		if(file != null){
			String oldFileName = file.getOriginalFilename() ;
			if(oldFileName != null && oldFileName.length() > 0){
				String img_path = contextPath + "/" + "img/" ;
				String newFileName = UUID.randomUUID() + oldFileName.substring(oldFileName.lastIndexOf(".")) ;
				File img_file = new File(img_path + newFileName) ;
				file.transferTo(img_file) ;
				itemsCustom.setPic(newFileName);
			}
		}

		itemsService.updateItems(id, itemsCustom);
		
		return "redirect:queryItems.action" ;
	}
	
	@RequestMapping("/deleteItems")
	public String deleteItems(Integer[] item_id){
		
		return "success" ;
	}
	
	@RequestMapping("/editItemsList")
	public String editItemsList(Model model,ItemsQueryVo itemsQueryVo) throws Exception{
		List<ItemsCustom> itemsList = itemsService.findItemsList(itemsQueryVo); 
		
		model.addAttribute("itemsList", itemsList) ;
		
		return "items/editItemsList";
	}
	
	@RequestMapping("/editItemsListSubmit")
	public String editItemsListSubmit(ItemsQueryVo vo) throws Exception{
		
		return "success";
		
	}
	
	@RequestMapping("/itemsView/{id}")
	public @ResponseBody ItemsCustom itemsView(@PathVariable("id") Integer id) throws Exception{
		ItemsCustom itemsCustom = itemsService.findById(id);
		return itemsCustom ;
	}
}
